package com.ruoyi.web.controller.system;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.system.domain.BBiddingQy;
import com.ruoyi.system.domain.SysArea;
import com.ruoyi.system.service.ISysAreaService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BBidding;
import com.ruoyi.system.service.IBBiddingService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 投标保函Controller
 * 
 * @author ruoyi
 * @date 2024-10-16
 */
@Controller
@RequestMapping("/system/bidding")
public class BBiddingController extends BaseController
{
    private String prefix = "system/bidding";

    @Autowired
    private IBBiddingService bBiddingService;
    @Autowired
    private ISysAreaService areaService;

    @RequiresPermissions("system:bidding:view")
    @GetMapping()
    public String bidding()
    {
        return prefix + "/bidding";
    }

    /**
     * 查询投标保函列表
     */
    @RequiresPermissions("system:bidding:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BBidding bBidding)
    {
        startPage();
        List<BBidding> list = bBiddingService.selectBBiddingList(bBidding);
        return getDataTable(list);
    }

    /**
     * 导出投标保函列表
     */
    @RequiresPermissions("system:bidding:export")
    @Log(title = "投标保函", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BBidding bBidding)
    {
        List<BBidding> list = bBiddingService.selectBBiddingList(bBidding);
        ExcelUtil<BBidding> util = new ExcelUtil<BBidding>(BBidding.class);
        return util.exportExcel(list, "投标保函数据");
    }

    /**
     * 新增投标保函
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存投标保函
     */
    @RequiresPermissions("system:bidding:add")
    @Log(title = "投标保函", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BBidding bBidding)
    {
        return toAjax(bBiddingService.insertBBiddingAndQiye(bBidding));
    }

    /**
     * 表单
     */
    @GetMapping("/form")
    public String form(BBiddingQy bBiddingQy)
    {
        System.out.println("kankan是多少"+bBiddingQy.toString());
        return prefix + "/form";
    }

    /**
     * 修改投标保函
     */
    @RequiresPermissions("system:bidding:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BBidding bBidding = bBiddingService.selectBBiddingById(id);
        mmap.put("bBidding", bBidding);
        return prefix + "/edit";
    }

    /**
     * 修改保存投标保函
     */
    @RequiresPermissions("system:bidding:edit")
    @Log(title = "投标保函", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BBidding bBidding)
    {
        return toAjax(bBiddingService.updateBBidding(bBidding));
    }

    /**
     * 删除投标保函
     */
    @RequiresPermissions("system:bidding:remove")
    @Log(title = "投标保函", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bBiddingService.deleteBBiddingByIds(ids));
    }

    /**
     * 模拟数据
     */
    @GetMapping("/cityData")
    @ResponseBody
    public String cityData()
    {
        JSONArray jsonArray = new JSONArray();
        SysArea area = new SysArea();
        area.setType("2");
        List<SysArea> shengList = areaService.selectSysAreaList(area);
        for (SysArea sheng:shengList){
            JSONArray jsonArray1 = new JSONArray();
            JSONObject object = new JSONObject();
            SysArea sysArea = new SysArea();
            sysArea.setParentId(sheng.getId());
            sysArea.setType("3");//市
            List<SysArea> shiList = areaService.selectSysAreaList(sysArea);
            for (SysArea shi:shiList){
                JSONObject object1 = new JSONObject();
                object1.put("n",shi.getSimpName());
                jsonArray1.add(object1);
            }
            object.put("n",sheng.getSimpName());
            object.put("s",jsonArray1);
            jsonArray.add(object);
        }
        return jsonArray.toString();
    }
}
